﻿using FinancialReportPowerPointAddIn.Helper;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Drawing;
using System.Linq;
using PpChartType = Microsoft.Office.Core.XlChartType;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace FinancialReportPowerPointAddIn.PPTModule
{
    public static class GenerateSinglePlatformSalesTrend
    {
        public static void Write(int year, int month, string target)
        {
            Common.GetPLNameAndCode(target, out string flag, out string code);
            //获取当前ppt中所有的幻灯片
            var slides = Globals.ThisAddIn.Application.ActivePresentation.Slides;
            //在当前选中幻灯片前插入新的幻灯片--代码提示中没有F12进去才找到的方法
            Slide currentslide = slides.Add(slides.Count + 1, PpSlideLayout.ppLayoutBlank);
            currentslide.Select();
            //开始写入数据 
            using (var db = new FinancialReportContext())
            {
                //写入平台差异图表                
                Chart chart = currentslide.Shapes.AddChart2(
                    Type: PpChartType.xlLineMarkers,
                    Left: 0,
                    Top: 0,
                    Width: currentslide.Master.Width,
                    Height: currentslide.Master.Height,
                    NewLayout: true).Chart;
                // 设置图表的数据
                // 获取图表的数据源工作表  
                var worksheet = chart.ChartData.Workbook.Worksheets[1];
                // 清除之前的数据
                worksheet.UsedRange.Clear();
                // 修改工作表中的数据
                worksheet.Cells[1, 1].Value = null;
                worksheet.Cells[2, 1].Value = "全店";
                worksheet.Cells[3, 1].Value = "FC";
                worksheet.Cells[4, 1].Value = "MDS";
                worksheet.Cells[5, 1].Value = "内甜";
                List<string> platforms = new List<string> { "全店", "FC", "MDS", "内甜" };
                if (flag == "潭江")
                {
                    worksheet.Cells[6, 1].Value = "外甜";
                    platforms.Add("外甜");
                }
                int row = 2;
                int col = 2;
                for (int i = -12; i <= 0; i++)
                {
                    DateTime date = new DateTime(year: year, month: month, day: 1).AddMonths(i);
                    worksheet.Cells[row-1, col].Value = $"{date.Month}月";
                    var cpldata = db.PLDatas.FirstOrDefault(x => x.Restaurant == flag && x.Year == date.Year && x.Month == date.Month && x.IsPlan == false);
                    var lypldata = db.PLDatas.FirstOrDefault(x => x.Year == date.Year - 1 && x.Month == date.Month && x.Restaurant == flag && x.IsPlan == false);
                    var cplatformdata = db.PlatformSales.FirstOrDefault(x => x.Uscode == code && x.Year == date.Year && x.Month == date.Month);
                    var lyplatformdata = db.PlatformSales.FirstOrDefault(x => x.Uscode == code && x.Year == date.Year - 1 && x.Month == date.Month);                    
                    foreach (var item in platforms)
                    {                        
                        switch (item)
                        {
                            case "全店":
                                worksheet.Cells[row, col].Value = Calculate.CalVar(cpldata.营业净收入, lypldata.营业净收入).ToString("p");
                                break;
                            case "FC":
                                worksheet.Cells[row, col].Value = Calculate.CalVar(cplatformdata.FC营业净收入, lyplatformdata.FC营业净收入).ToString("p");
                                break;
                            case "MDS":
                                worksheet.Cells[row, col].Value = Calculate.CalVar(cplatformdata.MDS营业净收入, lyplatformdata.MDS营业净收入).ToString("p");
                                break;
                            case "内甜":
                                worksheet.Cells[row, col].Value = Calculate.CalVar(cplatformdata.Kiosk营业净收入, lyplatformdata.Kiosk营业净收入).ToString("p");
                                break;
                            case "外甜":
                                worksheet.Cells[row, col].Value = Calculate.CalVar(cplatformdata.RemoteKiosk营业净收入, lyplatformdata.RemoteKiosk营业净收入).ToString("p");
                                break;
                        }
                        row++;
                    }
                    row = 2;
                    col++;
                }
                if (flag == "潭江")
                {
                    row = 6;
                }
                else
                {
                    row = 5;
                }
                // 设置图表的数据范围            
                string rangeAddress = "='Sheet1'!" + worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[row, 14]].Address;
                chart.SetSourceData(rangeAddress, XlRowCol.xlRows);
                // 保存并关闭Excel工作簿
                chart.ChartData.Workbook.Close(true);
                // 添加图表标题
                chart.HasTitle = true;
                chart.ChartTitle.Text = $"{flag}-各平台Sales滚动趋势";
                // 添加图例
                chart.HasLegend = true;
                //设置系列1的填充颜色为红色
                chart.SeriesCollection(1).Format.Line.ForeColor.RGB = ColorTranslator.ToOle(Color.Black);
                chart.SeriesCollection(2).Format.Line.ForeColor.RGB = ColorTranslator.ToOle(Color.Red);
                chart.SeriesCollection(3).Format.Line.ForeColor.RGB = ColorTranslator.ToOle(Color.DarkGoldenrod);
                chart.SeriesCollection(4).Format.Line.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(148,0,211));
                //设置线段类型                
                chart.SeriesCollection(2).Format.Line.DashStyle = Microsoft.Office.Core.MsoLineDashStyle.msoLineSysDash;
                chart.SeriesCollection(3).Format.Line.DashStyle = Microsoft.Office.Core.MsoLineDashStyle.msoLineSysDot;
                chart.SeriesCollection(4).Format.Line.DashStyle = Microsoft.Office.Core.MsoLineDashStyle.msoLineSysDash;
                if (flag == "潭江")
                {
                    chart.SeriesCollection(5).Format.Line.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(72,118,255));
                    chart.SeriesCollection(5).Format.Line.DashStyle = Microsoft.Office.Core.MsoLineDashStyle.msoLineSysDot;
                }
                // 取消横向网格线
                chart.Axes(XlAxisType.xlValue).HasMajorGridlines = false;
                chart.Axes(XlAxisType.xlValue).HasMinorGridlines = false;
                // 取消纵向网格线
                chart.Axes(XlAxisType.xlCategory).HasMajorGridlines = false;
                chart.Axes(XlAxisType.xlCategory).HasMinorGridlines = false;
                //数据绘图区填充颜色
                chart.PlotArea.Format.Fill.ForeColor.RGB = System.Drawing.Color.FromArgb(233, 233, 238).ToArgb();                
                // 设置整个图表的字体大小
                chart.ChartArea.Format.TextFrame2.TextRange.Font.Size = 10;
                // 设置图表标题字体大小
                chart.ChartTitle.Format.TextFrame2.TextRange.Font.Size = 16;
                chart.ChartTitle.Format.TextFrame2.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoTrue;
                // 显示图表的数据表
                chart.HasDataTable = true;

                // 设置水平轴刻度线的颜色和线条厚度
                Axis xAxis = chart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);                
                xAxis.Format.Line.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(0,139,139));
                xAxis.Format.Line.DashStyle = Microsoft.Office.Core.MsoLineDashStyle.msoLineSysDot;
                xAxis.Format.Line.Weight = 2;
            }
        }
    }
}
